﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpCommandBase.h"

#include "IDocument.h"
#include "DrawNewLevelAction.h"
#include "StructureCmdIds.h"
#include "GmUiStructureCommandRegister.h"
#include "IUiDocumentViewManager.h"
#include "IModelView.h"
#include "IUiView.h"
#include "Vector3dUtils.h"
#include "IPresetParameterManager.h"
#include "IGenericType.h"
#include "ILevelType.h"
#include "IUiDocument.h"
#include "GbmpBuiltInCategoryUniIdentitiesNew.h"
#include "IUserTransaction.h"
#include "GbmpDocUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace std;

//////////////////////////////////////////////////////////////////////////
class CmdDrawLineNewLevel : public GbmpCommandBase
{
public:
    CmdDrawLineNewLevel():GbmpCommandBase(ID_CMD_DRAWING_NEWLEVEL){};
    ~CmdDrawLineNewLevel(){};

public:
    virtual bool IsVisible() const override { return true; }
    virtual bool IsEnabled() const override;
    virtual OwnerPtr<IAction> ExecuteCommand(const gcmp::CommandParameters& cmdParams) override;
};

bool CmdDrawLineNewLevel::IsEnabled() const
{
    if (!CommandBase::IsEnabled())
        return false;

    IUiDocumentViewManager* pUiDocViewMgr = IUiDocumentViewManager::Get();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pUiDocViewMgr, L"pUiDocViewMgr为空",L"GDMPLab",L"2024-03-30");

    IUiView* pUIView = pUiDocViewMgr->GetCurrentUiView();
    if (pUIView != nullptr)
    {
        IModelView* pModelView = pUIView->GetModelView();
        if (pModelView != nullptr)
        {
            Vector3d viewUp = pModelView->GetUpDirection();
            if (Vector3dUtils::IsParallel(viewUp, Vector3d::UnitZ, Constants::DOUBLE_EPS))
            {
                return true;
            }
        }
    }

    return false;
}

OwnerPtr<IAction> CmdDrawLineNewLevel::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    IUiDocumentViewManager* pUiDocViewMgr = IUiDocumentViewManager::Get();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiDocViewMgr, L"pUiDocViewMgr为空",L"GDMPLab",L"2024-03-30");
    IDocument* pDoc = pUiDocViewMgr->GetCurrentUiDocument()->GetDbDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"文档为空",L"GDMPLab",L"2024-03-30");

    IPresetParameterManager* pPresetMgr = IPresetParameterManager::Get(pDoc);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pPresetMgr, L"pPresetMgr为空",L"GDMPLab",L"2024-03-30");

    ElementId typeId;
    //if (pPresetMgr->GetPresetType(BuiltInCategoryUniIdentities::BICU_NEW_ELEVATION, 0, typeId) == false)
    {
        std::vector<IElement*> pElements = gcmp::GbmpDocUtils::GetElementsByCategory(pDoc, BuiltInCategoryUniIdentities::BICU_NEW_LEVEL_TYPE);
        FOR_EACH(pElement, pElements)
        {
            if (IGenericType* pType = quick_cast<IGenericType>(pElement))
            {
                typeId = pType->GetElementId();
                break;
            }
        }

        if (typeId.IsValid() == false)
        {
            OwnerPtr<IUserTransaction> opUt = IUserTransaction::Create(pDoc, L"创建标高类型");
            ILevelType* pLevelType = ILevelType::Create(pDoc, GBMP_TR(L"标准标高"));
            DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pLevelType, L"标准标高创建失败",L"GDMPLab",L"2024-03-30");
            typeId = pLevelType->GetOwnerElement()->GetElementId();
            opUt->Commit();
        }
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(typeId.IsValid(), L"标高类型不合法",L"GDMPLab",L"2024-03-30");
    }

    return NEW_AS_OWNER_PTR(DrawNewLevelAction, typeId);;
}

REGISTER_GM_COMMAND(CmdDrawLineNewLevel);
